package com.ting.test.algorithms.stack;

import java.util.Stack;

/**
 * 队列:
 * 先进先出
 */
public class 用栈实现队列 {

    public static void main(String[] args) {
        int[] arr = new int[]{11, 2, 4, 6, 8, 9, 0, 6};
        MyQueue stack = new MyQueue();
        for (int s : arr) {
            boolean push = stack.add(s);
            System.out.println("push:" + s + "," + push);
        }

        for (int s : arr) {
            Integer pop = stack.get();
            System.out.println("pop:" + pop );
        }
    }


    static class MyQueue {
        Stack<Integer> pushStack = new Stack<>();
        Stack<Integer> popStack = new Stack<>();

        public boolean add(int param) {
            pushStack.push(param);
            return true;
        }

        /**
         * 当pop中有数据时，直接pop
         * 当pop为空时 将push中的数据全部倒入pop中
         * @return
         */
        public int get() {
            if (popStack.isEmpty()) {
                while (!pushStack.isEmpty()) {
                    popStack.push(pushStack.pop());
                }

            }
            return popStack.pop();
        }

    }
}
